function init(){
    addimg.src = picsrc1;
    addimghover.src = picsrc2;
    findimg.src = findpicsrc1;
    findimghover.src = findpicsrc2;
    shareimg.src = sharepicsrc1;
    shareimghover.src = sharepicsrc2;

    addimgBtn.showTip("新建笔记");
    findimgBtn.showTip("查找笔记");
    shareimgBtn.showTip("分享笔记");

    s1.innerHTML = currentUser.tel;
}

//1.初始化页面，主要给页面的层或图片赋值
init();
var isLoad = false;
var isover = false;
var p = 0;
function f() {
    if(!isLoad && !isover) {
        p++;
        loading2.style.display = "block";
        isLoad = true;
        ajax("/biji/all2", {"page": p, "size": 10, "sortNum": 1},
            function (data) {
            //d2.innerHTML = "";
            setTimeout(function () {
                var totalPages = data.totalPages;
                data = data.content;

                if (p == totalPages) {
                    isover = true;
                    d4.innerHTML = "我也是有底线的！"
                }

                for (let i = 0; i < data.length; i++) {
                    var newDiv = document.createElement("div");
                    newDiv.onclick = function(){
                        newDivClick(data[i]);
                    };
                    newDiv.className = "biji clearfix";
                    var leftDiv = document.createElement("div");
                    leftDiv.className = "left fl";
                    var titleDiv = document.createElement("div");
                    titleDiv.className = "biji_title";
                    var titleTxt = data[i].title;
                    if (titleTxt) {
                        titleDiv.innerHTML = titleTxt.length < 15 ? titleTxt : titleTxt.substring(0, 15) + "...";
                        leftDiv.appendChild(titleDiv);
                    }

                    var dateDiv = document.createElement("div");
                    dateDiv.className = "biji_date";
                    var createdate = data[i].createDate;
                    if (createdate) {
                        var arr = createdate.split(/[\/: ]/);
                        if (arr.length == 6) {
                            var time1 = new Date(arr[0], arr[1] - 1, arr[2], arr[3].substring(2), arr[4], arr[5]).getTime();
                            var time2 = new Date().getTime();
                            var diff = time2 - time1;
                            if (diff < 0) {
                                dateDiv.innerHTML = "时空穿越";
                            } else if (diff < 5 * 1000) {
                                dateDiv.innerHTML = "刚刚";
                            } else if (diff < 60 * 1000) {
                                dateDiv.innerHTML = Math.floor(diff / 1000) + "秒前";
                            } else if (diff < 60 * 60 * 1000) {
                                dateDiv.innerHTML = Math.floor(diff / (1000 * 60)) + "分钟前";
                            } else if (diff < 60 * 60 * 1000 * 24) {
                                dateDiv.innerHTML = Math.floor(diff / (1000 * 60 * 60)) + " 小时前";
                            } else if (diff < 60 * 60 * 1000 * 24 * 30) {
                                dateDiv.innerHTML = Math.floor(diff / (1000 * 60 * 60 * 24)) + " 天前";
                            } else if (diff < 60 * 60 * 1000 * 24 * 30 * 365) {
                                dateDiv.innerHTML = Math.floor(diff / (1000 * 60 * 60 * 24 * 30)) + " 月前";
                            } else if (diff < 60 * 60 * 1000 * 24 * 30 * 365 * 100) {
                                dateDiv.innerHTML = Math.floor(diff / (1000 * 60 * 60 * 24 * 30 * 365)) + " 年前";
                            }

                            leftDiv.appendChild(dateDiv);
                        }

                    }

                    var contentDiv = document.createElement("div");
                    contentDiv.className = "biji_content";
                    var cnt = data[i].content;
                    var txt = data[i].contextTxt;
                    if (!txt) {
                        txt = cnt;
                    }
                    contentDiv.innerHTML = txt.length < 30 ? txt : txt.substring(0, 30) + "...";
                    leftDiv.appendChild(contentDiv);
                    newDiv.appendChild(leftDiv);

                    var rightDiv = document.createElement("div");
                    rightDiv.className = "right fl";
                    if (cnt.indexOf("<img") != -1) {
                        var start = cnt.indexOf("<img");//383 538
                        var end = cnt.indexOf(">", start);
                        var imgHTML = cnt.substring(start, end + 1);
                        var start2 = imgHTML.indexOf("src=\"");
                        var end2 = imgHTML.indexOf("\"", start2 + 5);
                        var picsrc = imgHTML.substring(start2 + 5, end2);
                        rightDiv.style.background = "url(" + picsrc + ") no-repeat center center";
                        rightDiv.style.backgroundSize = "cover";
                        newDiv.appendChild(rightDiv);
                    } else {
                        leftDiv.style.width = "100%";
                    }

                    var deleteEle = document.createElement("button");
                    deleteEle.innerHTML = "删除";
                    deleteEle.style.display = "block";
                    deleteEle.style.clear = "both";

                    deleteEle.onclick = function () {
                        deleteBiJi(data[i].id);
                    };
                    newDiv.appendChild(deleteEle);
                    d2.appendChild(newDiv);
                }
                if(document.getElementById("loading2")) {
                    loading2.style.display = "none";//和20行呼应
                }
                isLoad = false;

            }, 100);

        })
    }
}
f();
window.onscroll = function () {
    var sh = document.documentElement.scrollHeight;
    var ch = document.documentElement.clientHeight;
    var st = document.documentElement.scrollTop ||
        document.body.scrollTop;

    if (sh < st + ch + 100) {
        f();
    }
};

addimgBtn.onclick = function(){
    fre.style.display = "none";
    d3.style.display = "block";
    title.focus();
};

finishBtn.onclick = function(){
    loading.style.display = "block";
    var titleValue = title.value;
    var contentValue = content.innerHTML;
    var contextTxt = content.innerText.trim();

    if(titleValue.length==0||contentValue.length==0){
        alert("请填写内容");
        title.focus();//获得焦点
        return;
    }

    ajax("/biji/add",{
        "biJiUser": {
            "id": currentUser.id
        },
        "content": contentValue,
        "createDate": new Date().toLocaleString(),
        "title": titleValue,
        "contextTxt":contextTxt
    },function(data){
        if(data){
            fre.style.display = "block";
            d3.style.display = "none";
            loading.style.display = "none";
            title.value = "";
            content.innerHTML = "";
            d2.innerHTML = "<div>新数据</div>" + d2.innerHTML;
        }
    });
};
content.onfocus = function(){
    contentTip.style.display = "none";
};
content.onblur = function(){
    if(content.innerHTML.length==0){
        contentTip.style.display = "block";
    }
};
returnBtn.onclick = function(){
    fre.style.display = "block";
    d3.style.display = "none";
};
function deleteBiJi(id){
    if(confirm("确定要删除这个笔记？")){
        ajax("/biji/del",{id:id},function(data){
            if(data){
                f();
            }else{
                alert("删除失败！")
            }
        });
    }
}
var st;
function newDivClick(data){
    st = document.documentElement.scrollTop;
    console.log(data);
    fre.style.display = "none";
    d3.style.display = "block";
    title.value = data.title;
    content.innerHTML = data.content;
    contentTip.style.display = "none";
    document.documentElement.scrollTop = 0;
    addOrUpdate.innerHTML = "编辑";
}